package com.zero.execute.writer;

import com.zero.core.ZeroContext;
import com.zero.function.ToGet;
import com.zero.util.BaseUtil;
import com.zero.util.CamelCaseUtil;
import com.zero.util.FreemarkerUtils;
import com.zero.xmodel.definition.DaoDefinition;
import com.zero.xmodel.definition.EntityDefinition;
import com.zero.xmodel.definition.ServiceDefinition;

import java.util.HashMap;
import java.util.Map;

/**
 * 生成service
 *
 * @author wuxi
 * @date 2020-07-29
 */
public class ServiceWriter implements Writer {

    private ZeroContext context;

    public ServiceWriter(ZeroContext context) {
        this.context = context;
    }

    @Override
    public void create() {
        Map<String, DaoDefinition> daoMap = this.context.getDaoMap();
        for (Map.Entry<String, DaoDefinition> entry : daoMap.entrySet()) {
            //构建service的定义对象
            ServiceDefinition definition = builderServiceDefinition(entry);
            //生成Service层
            generateService(definition);
        }

    }

    private void generateService(ServiceDefinition definition) {
        Map<String, Object> data = new HashMap<>(1);
        data.put("service", definition);
        data.put("toGet", new ToGet());
        String servicePath =
                this.context.getPackagePO().getServicePackageName() + this.context.getSeparator() + definition.getInterfaceClassName();
        //生成service
        FreemarkerUtils.getTemplate(this.context.getFreemarkerTemplateMap().get("serviceTemplate"),
                data,
                servicePath,
                this.context.getSeparator());
        //生成service实现
        String serviceImplPath = this.context.getPackagePO().getServicePackageName()
                + this.context.getSeparator() + "impl" + this.context.getSeparator() + definition.getClassName();
        FreemarkerUtils.getTemplate(this.context.getFreemarkerTemplateMap().get("serviceImplTemplate"),
                data,
                serviceImplPath,
                this.context.getSeparator());

    }

    /**
     * 构建service的定义对象
     *
     * @param entry 参数
     */
    private ServiceDefinition builderServiceDefinition(Map.Entry<String, DaoDefinition> entry) {
        String daoName = entry.getKey();
        DaoDefinition daoDefinition = entry.getValue();
        //service的定义对象
        ServiceDefinition definition = new ServiceDefinition();
        definition.setPackageName(BaseUtil.parsingPackage(this.context.getPackagePO().getServicePackageName(),
                this.context.getSeparator()));
        definition.setClassName(daoName.replace("Mapper", "ServiceImpl"));
        definition.setInterfaceClassName(daoName.replace("Mapper", "Service"));
        definition.setDaoClass(daoDefinition.getPackageName() + "." + daoName);
        definition.setDaoName(CamelCaseUtil.convertToCamelCase(daoName));
        definition.setDaoClassName(daoName);
        definition.setPojoClass(daoDefinition.getPojoClass());
        definition.setPojoName(daoDefinition.getPojoName());
        EntityDefinition entityDefinition = this.context.getEntityMap().get(daoDefinition.getPojoName());
        definition.setImportNames(entityDefinition.getImportNames());
        definition.setPojoAttr(entityDefinition.getAttrs());
        return definition;
    }

}